# 计算文件超过9M的地址
# -*- coding: utf-8 -*-
import os 
import time 
 
from os.path import join, getsize 
from heapq import nlargest 
 
 
def walk_files_and_sizes(start_at: str): 
    for root, _, files in os.walk(start_at): 
        for file in files: 
            path = join(root, file) 
            try: 
                size = getsize(path)  # bytes 
                yield path, size 
            except OSError: 
                continue 
 
 
def largest_files(n: int, start_at: str) -> None: 
    MB = 1024 * 1024 
    largest = nlargest(n, walk_files_and_sizes(start_at), key=lambda x: x[1]) 
 
    arrList = []
    for path, size in largest: 
        fileSize = size // MB
        if fileSize >=9:
            arrList.append(path)
            # print(f'{fileSize} MB {path}') 
            print(f'{path}') 
 
 
 
if __name__ == '__main__': 
    start = time.perf_counter() 
    largest_files(100, "E:/Code/UE/MR/") 
    elapsed = time.perf_counter() - start 
    print(f'{elapsed} seconds elapsed') 

